package com.bojx.cms.web;

import java.util.List;


import com.bojx.cms.service.ArticleService;
import com.jfinal.aop.Clear;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.render.JsonRender;

public class ArticleController extends BaseController {
	private static ArticleService articleService=new ArticleService();
	@Clear
	public void list(){
		String keyword=getPara("keyword");
		long count=articleService.count(keyword);
		long pageCount=count/20+1;
		setAttr("keyword",keyword);
		setAttr("pageCount", pageCount);
		render("list.html");
	}
	
	@Clear
	public void getPageData(){
		Record result=new Record();
		try{
			String keyword=getPara("keyword");
			int pageNum=getParaToInt("pageNum");
			long count=articleService.count(keyword);
			List<Record> data=articleService.queryArticles(pageNum,keyword);
			result.set("code", "00");
			result.set("count", count);
			result.set("msg", "查询成功");
			result.set("data", data);
		}catch(Exception e){
			LOG.error(e.getMessage(),e);
			result.set("code", "09");
			result.set("msg", "查询失败");
		}finally{
		    render(new JsonRender(result).forIE());
		}	
	}
	
	@Clear
	public void detail(){
		try{
			Long id=getParaToLong(0);
			if(id==null){
				throw new Exception("id为空");
			}
		    Record result=articleService.findArticleById(id);
		    setAttr("result", result);
		}catch(Exception e){
			LOG.error(e.getMessage(), e);
		}
		
		render("detail.html");
	}
}
